﻿/* $RCSFile: FloatingDockWindow.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Helpers/FloatingDockWindow.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Web.Script.Serialization;
using System.Globalization;

using AddonNice.Diagnostics;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.Helpers
{
    #region Dock management

    public class DockStateJavaScriptUnitConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
	        if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]DockStateJavaScriptUnitConverter Deserialize BEG.",CoreTraceSwitch.Sw.Info);
            if (dictionary == null)
            {
                const string msg  =   "[E]DockStateJavaScriptUnitConverter Deserialize dictinary null.";
                if (CoreTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine(msg, CoreTraceSwitch.Sw.Info);
                throw new ArgumentNullException(msg);
            }

            if (type == typeof(Unit))
            {
                double value        =   Convert.ToDouble(dictionary["Value"]);
                UnitType unitType   =   (UnitType)Enum.Parse(typeof(UnitType), dictionary["Type"].ToString());
                Unit unit           =   new Unit(value, unitType);
                return unit;
            }

	        if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DockStateJavaScriptUnitConverter Deserialize Type is not Unit: {0}.",type),CoreTraceSwitch.Sw.Info);
            return null;
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
	        if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]DockStateJavaScriptUnitConverter Serialize BEG.",CoreTraceSwitch.Sw.Info);
            Unit unit                           =   (Unit)obj;

            IDictionary<string, object> dict    =   new Dictionary<string, object>();
            if (unit != null)
            {
                dict.Add("Type", unit.Type);
                dict.Add("Value", unit.Value);
            }
	        if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]DockStateJavaScriptUnitConverter Serialize END.",CoreTraceSwitch.Sw.Info);
            return dict;
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(Unit) })); 
            }
        }
    }

    public class InternalDockState
    {
        bool _Collapsed      =   false;
        string _DockZoneID   =   string.Empty;
        int _Index;
        Unit _Left;
        Unit _Top;
        Unit _Width;
        string _UniqueName   =   string.Empty;
        bool _Pinned         =   false;

        public bool Collapsed 
        { 
            get
            {
                return _Collapsed;
            } 
            set
            {
                _Collapsed  =   value; 
            }
        }
        
        public string DockZoneID
        { 
            get
            {
                return _DockZoneID;
            } 
            set
            {
                if ( value != null )
                    _DockZoneID  =   value; 
            }
        }
        
        public int Index
        { 
            get
            {
                return _Index;
            } 
            set
            {
                _Index  =   value; 
            }
        }
        
        public Unit Left
        { 
            get
            {
                return _Left;
            } 
            set
            {
                _Left  =   value; 
            }
        }
        
        public Unit Top
        { 
            get
            {
                return _Top;
            } 
            set
            {
                _Top  =   value; 
            }
        }
        
        public Unit Width
        { 
            get
            {
                return _Width;
            } 
            set
            {
                _Width  =   value; 
            }
        }
        
        public string UniqueName
        { 
            get
            {
                return _UniqueName;
            } 
            set
            {
                if ( value != null )
                    _UniqueName  =   value; 
            }
        }
        
        public bool Pinned
        { 
            get
            {
                return _Pinned;
            } 
            set
            {
                _Pinned  =   value; 
            }
        }
        
        public InternalDockState(bool wCollapsed, string wDockZoneID, int wIndex, Unit wLeft, Unit wTop,
            string wUniqueName, bool wPinned,Unit wWidth)
        {
	        if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InternalDockState Ctor wCollapsed: {0}, DockZoneID: {1}, Index: {2}, Left: {3}, Top: {4}, UniqueName: {5}, Pinned: {6}, Width: {7}.",
                    new object[]{wCollapsed,wDockZoneID,wIndex,wLeft,wTop,wUniqueName,wPinned,wWidth}),CoreTraceSwitch.Sw.Info);
            _Collapsed      =   wCollapsed;
            _DockZoneID     =   wDockZoneID;
            _Index          =   wIndex;
            _Left           =   wLeft;
            _Top            =   wTop;
            _UniqueName     =   wUniqueName;
            _Pinned         =   wPinned;
            _Width          =   wWidth;
        }
        
        public InternalDockState()
        {
	        if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]InternalDockState Parameterless Ctor called.",CoreTraceSwitch.Sw.Info);
        }

        public override string ToString()
        {
            return string.Format("Collapsed: {0}, DockZoneID: '{1}', Index: {2}, Left: {3}, Top: {4}, UniqueName: '{5}', Pinned: {6}, Width: {7}.",
                    new object[]{Collapsed,DockZoneID,Index,Left,Top,UniqueName,Pinned,Width});
        }
    }

    #endregion
}
